<template>
  <d2-container>
    <div class="d2-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" class="SearchForm">
    
      <el-form-item label="角色名称">
        <el-input
          placeholder="请输入"
          type="text"
          v-model.trim="searchForm.roleName"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="角色状态">
        <el-select v-model="searchForm.status" placeholder="请选择">
          <el-option label="正常" :value="0"></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="添加日期">
        <el-date-picker
          v-model="searchForm.createTime"
          type="date"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
      <el-button type="primary" @click="search()" icon="el-icon-search" 
        >查询</el-button
      >
      <el-button type="danger" icon="el-icon-refresh" @click="reset" 
        >重置</el-button
      >
       <router-link to="/systemAdmin/role/add">
          <el-button type="success" style="margin:0 10px 10px 10px">新增角色</el-button>
        </router-link>
      </el-form-item>
    </el-form>

    <el-table border :data="tableData.list">
      <el-table-column
        label="角色ID"
        align="center"
        prop="roleId"
      ></el-table-column>
      <el-table-column
        label="角色名称"
        align="center"
        prop="roleName"
      ></el-table-column>

      <el-table-column label="添加时间" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ $moment(scope.row.createTime).format("YYYY-MM-DD hh:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column label="角色状态" align="center">
        <template slot-scope="scope">
          {{ ["正常", "停用"][scope.row.status] }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <router-link :to="`/systemAdmin/role/edit/${scope.row.roleId}`">
            <el-button type="text">角色 查看/编辑</el-button>
          </router-link>
          <br>
          <router-link :to="`/systemAdmin/roletree/edit/${scope.row.roleId}`">
            <el-button type="text">权限 查看/编辑</el-button>
          </router-link>
          <br>
          <el-button
            type="text"
            class="margin-left"
            @click="deleteInfo(scope.row.roleId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="search"
      @current-change="search"
      :current-page.sync="page"
      :page-sizes="[10, 25, 50, 100]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
    ></el-pagination>
  </d2-container>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      query: {},
      searchForm: {},
      tableData: {
        list: [],
        total: 0,
      },
      page: 1,
      pageSize: 10,
      rules: {},
    };
  },
  methods: {
    search() {
      let params = {
        pageNum: this.page,
        pageSize: this.pageSize,
        ...this.searchForm,
      };
      if (params.createTime) {
        params.createTime = new Date(
          Date.parse(new Date(params.createTime)) + 8 * 3600 * 1000
        );
      }

      this.$axios.post("/sysRole/list", params).then((result) => {
        if (result) {
          this.tableData = {
            list: result.data.list,
            total: result.data.total,
          };
        } else {
          this.$message.error(result.data.msg);
        }
      });
    },
    reset() {
      this.searchForm = {};
      this.search();
    },
    deleteInfo(id) {
      const _this = this;
      this.$confirm("确定删除该条数据？", "确认删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnPressEscape: false,
        closeOnClickModal: false,
      }).then(() => {
        _this.$axios.delete(`/sysRole/delete/${id}`).then((res) => {
          if (res.code == 200) {
           
              _this.$message({
              message: "删除成功",
              type: "success",
            });
            _this.search();
          } else {
           _this.$message({
              message: res.data.msg,
              type: "error",
            });
          }
        });
      });
    },
  },
  created() {
    this.search();
  },
};
</script>

<style scoped lang="scss">
</style>
